//<script>
$(document).ready(function() {
    var active_id_groupe = $("#id_groupe").val();

    var is_request = false; // when ajax request sent its true. response makes it false
    
    insert_index = 0;

    $(".delete_option").live('click', function(){
        $(this).parent("tbody").append('<input type="hidden" data-type="delete" data-id="'+$(this).attr("data-element-id")+'" />');
        $(this).parent().parent().remove();
    });
// add new group
    $('.list_form .add_field_button').click(function(){
        var container = $('#proprietes');
        var content = '<tr class="t_propriete-'+active_id_groupe+'"><td>?</td><td>'
                    +'<input type="text" name="elements_insert['+insert_index+'][titre]" value="" style="width: 250px;" />'
                    +'<input type="hidden" name="elements_insert['+insert_index+'][id_groupe]" value="'+active_id_groupe+'"/>'
                    +'</td><td>&nbsp;</td><td>'
                    +'<select name="elements_insert['+insert_index+'][type]" style="width: 100px;" >'
                    +'<option value="checkbox"><?php echo(_T('ecatalogue:propriete_type_checkbox')); ?></option>'
                    +'<option value="select"><?php echo(_T('ecatalogue:propriete_type_select')); ?></option>'
                    +'<option value="radio"><?php echo(_T('ecatalogue:propriete_type_radio')); ?></option></select>'
                    +'</td><td><a href="javascript:;" class="t_delete_link"><?php echo(_T('ecatalogue:delete')); ?></a></td></tr>'
                    +'<tr class="t_soustitre-'+insert_index+'"><td>&nbsp;</td><td colspan="4">'
                    +'<?php echo(_T('ecatalogue:propriete_soustitre')); ?><input type="text" name="elements_insert['+insert_index+'][soustitre]" value="" style="width: 98%;" /></td></tr>'
                    +'<tr class="t_texte-'+insert_index+'"><td>&nbsp;</td><td colspan="4">'
                    +'<?php echo(_T('ecatalogue:propriete_texte')); ?><input type="text" name="elements_insert['+insert_index+'][texte]" value="" style="width: 98%;" /></td></tr>';
        insert_index++;
        container.append(content);
    });

// delete group
    $('.list_form .delete_link').live('click', function(){
        if (confirm('<?php echo(_T('ecatalogue:delete_element')); ?> "' + $(this).parent().parent().find('input').val() + '"?')) {                
            var element_id = $(this).attr('data-element-id');
            if (element_id!=undefined) {                                     
                $('.list_form').append('<input type="hidden" name="elements_delete['+element_id+']" value="'+element_id+'" />');    
            }
            $(this).parent().parent().remove(); //bad idea
        }                
    });

    $('.list_form .t_delete_link').live('click', function(){
        if (confirm('<?php echo(_T('ecatalogue:delete_element')); ?> "' + $(this).parent().parent().find('input').val() + '"?')) {
            $(this).parent().parent().next().remove(); // soustitre
            $(this).parent().parent().next().remove(); // texte
            $(this).parent().parent().remove();
        }
    });
    $('.reponse_formulaire').delay(3000).fadeOut();// 


// change groupe
   $("#id_groupe").live("change", function() {

        active_id_groupe = $(this).val();

        if(!is_request) {
            is_request = true;
            $.post('#CHEMIN{formulaires/ecatalogue_proprietes/ecatalogue_proprietes_ajax.php}',
                {
                    id_groupe: active_id_groupe
                },function(data) {

                    data = $.parseJSON(data);
                    is_request = false;

                    tmp = '';                                            
                    $.each(data, function(key, value){
                        tmp += '<tr class="propriete-'+value.id_propriete+'"><td class="id_propriete">'+value.id_propriete+'</td><td>'
                            +'<input type="hidden" class="rubrique_propriete" name="elements_update['+value.id_propriete+'][id_rubrique]" value="'+active_id_groupe+'" />   '
                            +'<input type="text" class="titre_propriete" name="elements_update['+value.id_propriete+'][titre]" value="'+value.titre+'" style="width: 250px;" />'
                            +'</td><td><a href="#" data-element-id="'+value.id_propriete+'" class="details">details</a></td><td>'
                            +'<select class="type_propriete" name="elements_update['+value.id_propriete+'][type]" style="width: 100px;" > '
                                +'<option value="checkbox"'+((value.type=="checkbox")?' selected="selected"':'')+'><?php echo(_T('ecatalogue:propriete_type_checkbox')); ?></option>'
                                +'<option value="select"'+((value.type=="select")?' selected="selected"':'')+'><?php echo(_T('ecatalogue:propriete_type_select')); ?></option>'
                                +'<option value="radio"'+((value.type=="radio")?' selected="selected"':'')+'><?php echo(_T('ecatalogue:propriete_type_radio')); ?></option>'
                            +'</select></td><td><a href="#" data-element-id="'+value.id_propriete+'" class="delete_link"><?php echo(_T('ecatalogue:delete')); ?></a></td></tr>'
                            +'<tr class="soustitre-'+value.id_propriete+'" style="display: none;"><td>&nbsp;</td><td colspan="4">'
                            +'<?php echo(_T('ecatalogue:propriete_soustitre')); ?><input type="text" name="elements_update['+value.id_propriete+'][soustitre]" value="'+value.soustitre+'" style="width: 98%;" /></td></tr>'
                            +'<tr class="texte-'+value.id_propriete+'" style="display: none;"><td>&nbsp;</td><td colspan="4">'
                            +'<?php echo(_T('ecatalogue:propriete_texte')); ?><input type="text" name="elements_update['+value.id_propriete+'][texte]" value="'+value.texte+'" style="width: 98%;" /></td></tr>';
                    });
                    $("#proprietes").empty();
                    if(tmp!='') $("#proprietes").append(tmp);
                }
            );    
        }                              
    });

// details propriete
    $('.details').live('click', function(){
        var propriete_id = $(this).attr("data-element-id");
        $(".soustitre-"+propriete_id).toggle();
        $(".texte-"+propriete_id).toggle();
        return false;
    });

});
//</script>